package stu.day25_day32.day25.day1;

import stu.day25_day32.day25.all.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;


public class LeetCode199 {
    public int findBottomLeftValue(TreeNode root) {

        Queue<TreeNode> queue = new LinkedList<>();

        queue.offer(root);
        int num = 0;

        while (!queue.isEmpty()){
            int size = queue.size();

            for(int i=0;i<size;i++){
                TreeNode t = queue.poll();
                if(t.left!=null){
                    queue.offer(t.left);
                }
                if(t.right!=null){
                    queue.offer(t.right);
                }
                if(i==0){
                    num = t.val;
                }


            }
        }

        return num;
    }

    public List<Integer> rightSideView(TreeNode root) {

        List<Integer> list = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();

        if(root==null){
            return list;
        }

        queue.offer(root);

        while (!queue.isEmpty()){
            int size = queue.size();
            int num = 0;
            for(int i=0;i<size;i++){
                TreeNode t = queue.poll();
                if(t.left!=null){
                    queue.offer(t.left);
                }
                if(t.right!=null){
                    queue.offer(t.right);
                }
                num = t.val;

            }

            list.add(num);

        }

        return list;
    }



    public List<Integer> largestValues(TreeNode root) {

        List<Integer> list = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();

        if(root==null){
            return list;
        }

        queue.offer(root);

        while (!queue.isEmpty()){
            int size = queue.size();
            int num = Integer.MIN_VALUE;
            for(int i=0;i<size;i++){
                TreeNode t = queue.poll();
                if(t.left!=null){
                    queue.offer(t.left);
                }
                if(t.right!=null){
                    queue.offer(t.right);
                }
                num = Math.max(num,t.val);

            }

            list.add(num);

        }

        return list;

    }

    public int maxLevelSum(TreeNode root) {
        int maxNum = 0;
        int len = 1;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int max = Integer.MIN_VALUE;

        while (!queue.isEmpty()){
            int size = queue.size();
            int num = 0;
            for(int i=0;i<size;i++){
                TreeNode t = queue.poll();
                if(t.left!=null){
                    queue.offer(t.left);
                }
                if(t.right!=null){
                    queue.offer(t.right);
                }
                num += t.val;
            }
            if(num>max){
                max = num;
                maxNum = len;
            }
            len += 1;

        }

        return maxNum;
    }

    public int deepestLeavesSum(TreeNode root) {


        int num =0;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int max = Integer.MIN_VALUE;

        while (!queue.isEmpty()){
            int size = queue.size();
            num = 0;
            for(int i=0;i<size;i++){
                TreeNode t = queue.poll();
                if(t.left!=null){
                    queue.offer(t.left);
                }
                if(t.right!=null){
                    queue.offer(t.right);
                }
                num += t.val;
            }

        }

        return num;
    }



}
